package com.zxinlee.account;

import com.zaxxer.hikari.HikariDataSource;
import io.seata.rm.datasource.DataSourceProxy;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;

import javax.sql.DataSource;

/**
 * 自定义的数据源自动配置类
 * 用来创建AT事务的数据源代理对象
 */
@Configuration
public class DSAutoConfiguration {

    /**
     * 数据源
     * ConfigurationProperties 注入配置参数
     * @return
     */
    @ConfigurationProperties(prefix = "spring.datasource")
    @Bean
    public DataSource dataSource(){
        return new HikariDataSource();//连接池
    }

    @Primary //首选对象
    @Bean
    public DataSource dataSourceProxy(DataSource dataSource){
        //import io.seata.rm.datasource.DataSourceProxy;
        return new DataSourceProxy(dataSource);
    }
}
